package com.hh.goodsmanage.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hh.baseframework.util.Check;
import com.hh.baseframework.util.dto.PageRange;
import com.hh.goodsmanage.bean.Goodsmanage;
import com.hh.goodsmanage.bean.GoodsmanageIn;
import com.hh.goodsmanage.bean.GoodsmanageOut;
import com.hh.goodsmanage.model.InventoryModel;
import com.hh.hibernate.util.dto.HQLParamList;
import com.hh.hibernate.util.dto.PagingData;
import com.hh.system.service.impl.BaseService;

@Service
public class GoodsmanageService extends BaseService<Goodsmanage> {
	@Autowired
	private GoodsmanageInService goodsmanageInService;
	@Autowired
	private GoodsmanageOutService goodsmanageOutService;

	@Override
	public PagingData<Goodsmanage> queryPagingData(Goodsmanage entity,
			PageRange pageRange) {
		return super.queryPagingData(
				entity,
				pageRange,
				new HQLParamList()
						.addConditionFilterEmpty(
								Restrictions.like("code",
										"%" + entity.getCode() + "%"))
						.addConditionFilterEmpty(
								Restrictions.like("name",
										"%" + entity.getName() + "%")));
	}

	public List<Map<String, Object>> viewInventory(String id) {
		List<GoodsmanageIn> goodsmanageInList = new ArrayList<GoodsmanageIn>();
		if (Check.isEmpty(id)) {
			goodsmanageInList = goodsmanageInService.queryAllList();
		} else {
			goodsmanageInList = goodsmanageInService.queryListByProperty(
					"goodsmanageId", id);
		}
		Map<String, InventoryModel> map = new HashMap<String, InventoryModel>();
		for (GoodsmanageIn goodsmanageIn : goodsmanageInList) {
			String key = goodsmanageIn.getColor() + "###"
					+ goodsmanageIn.getCodeNum();
			InventoryModel inventoryModel = map.get(key);
			if (inventoryModel == null) {
				inventoryModel = new InventoryModel();
				inventoryModel.setSum(goodsmanageIn.getNumber());
				inventoryModel.setZongjia(goodsmanageIn.getTotalPrice());
				map.put(key, inventoryModel);
			} else {
				inventoryModel.setSum(inventoryModel.getSum()
						+ goodsmanageIn.getNumber());
				inventoryModel.setZongjia(inventoryModel.getZongjia()
						+ goodsmanageIn.getTotalPrice());
				map.put(key, inventoryModel);
			}
		}

		List<GoodsmanageOut> goodsmanageOutList = new ArrayList<GoodsmanageOut>();
		if (Check.isEmpty(id)) {
			goodsmanageOutList = goodsmanageOutService.queryAllList();
		} else {
			goodsmanageOutList = goodsmanageOutService.queryListByProperty(
					"goodsmanageId", id);
		}

		Map<String, InventoryModel> map2 = new HashMap<String, InventoryModel>();
		for (GoodsmanageOut goodsmanageOut : goodsmanageOutList) {
			String key = goodsmanageOut.getColor() + "###"
					+ goodsmanageOut.getCodeNum();
			InventoryModel inventoryModel = map2.get(key);
			if (inventoryModel == null) {
				inventoryModel = new InventoryModel();
				inventoryModel.setSum(goodsmanageOut.getNumber());
				inventoryModel.setZongjia(goodsmanageOut.getTotalPrice());
				map2.put(key, inventoryModel);
			} else {
				inventoryModel.setSum(inventoryModel.getSum()
						+ goodsmanageOut.getNumber());
				inventoryModel.setZongjia(inventoryModel.getZongjia()
						+ goodsmanageOut.getTotalPrice());
				map2.put(key, inventoryModel);
			}
		}

		List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();

		Set<String> set = map.keySet();
		for (String key : set) {
			String[] keyStr = key.split("###");
			Map<String, Object> resultMap = new HashMap<String, Object>();

			InventoryModel inventoryModel = map.get(key);
			InventoryModel inventoryModel2 = map2.get(key);

			Integer inNum = inventoryModel.getSum();
			Integer outNum = inventoryModel2 == null ? 0 : inventoryModel2
					.getSum();

			Integer inZongjia = inventoryModel.getZongjia();
			Integer outZongjia = inventoryModel2 == null ? 0 : inventoryModel2
					.getZongjia();

			resultMap.put("color", keyStr[0]);
			resultMap.put("codeNum", keyStr[1]);
			resultMap.put("inNum", inNum);
			resultMap.put("outNum", outNum);
			resultMap.put("number", inNum - outNum);

			resultMap.put("inZongjia", inZongjia);
			resultMap.put("outZongjia", outZongjia);
			resultMap.put("zongjia", outZongjia - inZongjia);
			mapList.add(resultMap);
		}

		return mapList;
	}
}